namespace Demo.Api.Controllers;

using Microsoft.AspNetCore.Mvc;
using Demo.Services;
using Demo.DbContext;

[ApiController]
[Route("api/[controller]/[action]")]
public class UserApiController : Controller
{
    private readonly UserService _userService = null;
    public UserApiController()
    {
        _userService = new();
    }
    [HttpGet]
    public ActionResult<User>? Search(int id)// 此函数若未找到匹配ID则返回HTTP204
    {
        User? userID = _userService.GetByID(id);
        return (userID);
    }
    [HttpGet]
    public IActionResult? Search2(int id)// 此函数若为找到匹配ID则返回null
    {
        User? userID = _userService.GetByID(id);
        return Json(userID);
    }
    [HttpGet]
    public List<User> ListAll()
    {
        return _userService.Exproler();
    }
    /// <summary>
    /// 将IActionResult改为ActionResult可以解决无法传数字的问题
    /// 将枚举gender去掉之后才可以正常访问
    /// </summary>
    [HttpPost]
    public ActionResult<User> AddUser(User user)
    {
        _userService.Register(user);
        return Json(user);
    }
}
